# Maintainer: John Becker <john.becker@keysight.com>

_realname=libkqueue
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}")
pkgver=2.6.2
pkgrel=1
pkgdesc="Libkqueue (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://github.com/mheily/libkqueue'
msys2_repository_url=${url}
msys2_references=(
  'purl: pkg:github/mheily/libkqueue'
)
license=('spdx: ISC AND BSD-2-Clause')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=(
  "${MINGW_PACKAGE_PREFIX}-cmake"
  "${MINGW_PACKAGE_PREFIX}-cc"
  "${MINGW_PACKAGE_PREFIX}-ninja"
  'git'
)
source=(
  "git+${msys2_repository_url}.git#tag=v${pkgver}"
  0001-libkqueue-cmake-remove-werror.patch
  0002-libkqueue-cmake-fix-static-library-name.patch
  0003-libkqueue-cmake-fix-install-directory.patch
  0004-libkqueue-fix-pkgconfig-file.patch
)
sha256sums=('c62ede976fb29f20ed440e03e026f969e25e0b97b4e789f92b35d2282fc7af9f'
            'b51e1305975e4f5040e6a8256c44048652c719480ef2ddcafa25b1501c999111'
            '0f76a2ec2e39a03930a593ffb9f35d911b803388f930dc26178b2918c56747a4'
            '26f84fcc3c397f7a8f9b23898f5e2d2aa3e1fb557018cbc3d6842acdc167a929'
            '20acdc81ba86c27fca6506704b7fc718309c5b025b0ac2531b1a3652ccc382e2')

_apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -p1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${_realname}"

  _apply_patch_with_msg \
    0001-libkqueue-cmake-remove-werror.patch \
    0002-libkqueue-cmake-fix-static-library-name.patch \
    0003-libkqueue-cmake-fix-install-directory.patch \
    0004-libkqueue-fix-pkgconfig-file.patch
}

build() {
  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    cmake \
      -GNinja \
      -DCMAKE_INSTALL_PREFIX="${MINGW_PREFIX}" \
      "${_extra_config[@]}" \
      -S "${_realname}" \
      -B "build-${MSYSTEM}"

  cmake --build "build-${MSYSTEM}"
}

package() {
  DESTDIR="${pkgdir}" cmake --install "build-${MSYSTEM}"
  install -Dm644 "${srcdir}/${_realname}/LICENSE" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/LICENSE"
}
